import * as echarts from '../../ec-canvas/echarts';
var util = require('../../utils/util.js');
import { Request } from "../../utils/request.js"

var request = new Request()
const app = getApp();

function initChart(canvas, width, height) {

  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
  });

  canvas.setChart(chart);

  request.get(
    "getTodayInformation/" + app.globalData.openid + '/' + util.formatTimeTwo(new Date().getTime(), 'Y-M-D'),
    null,
    function(res){
      if(res.statusCode != 200){
        chart.setOption(getPieOption([{name:"暂无",value:100}]));
        return ;
      }
      console.log(res.data.result.pie);
      chart.setOption(getPieOption(res.data.result.pie));
    },
    function(res){
      chart.setOption(getPieOption([{name:"暂无",value:100}]));
    }
  )
  return chart;
}

function getPieOption(seriesData)
{
  var option = {
    backgroundColor: "#ffffff",
    color: ["#37A2DA", "#32C5E9", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
    title: {
      text: "学习时间分布",
      left: 'center'
    },
    
    series: [{
      type: 'pie',
      radius: '50%',
      center: ['50%', '50%'],
      data: seriesData,

      animationType: 'scale',
      animationEasing: 'elasticOut',
      animationDelay: function (idx) {
        return Math.random() * 200;
      },
      label: {
          position: 'outer',
          alignTo: 'labelLine',
          bleedMargin: 5
      },
  }]
  };
  return option;
}

module.exports = {
  initPie : initChart
}